//
//  ByNewsListVC.m
//  caipiao_ios
//
//  Created by mc on 2018/7/30.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByNewsListVC.h"
#import "ByNewsDetailVC.h"

@interface ByNewsListVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;



@end

@implementation ByNewsListVC

- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray arrayWithCapacity:1];
    }
    return _dataArray;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addLeftBackBarButtonItem];
    self.title = @"新闻资讯";
    
    [self initViews];
    
    [self loadData];
    ByCoreRefreshGifHeader *header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
    self.tableView.mj_header = header;
    // Do any additional setup after loading the view.
}

- (void)loadData{
    [ByCoreToolAlert showLoadind:@"加载中"];
    [ByFoundApi newsSuccess:^(ByNetBaseResult *result) {
        [ByCoreToolAlert hideHud];
        [self.tableView.mj_header endRefreshing];
        SLog(@"新闻资讯 = %@", result.data);
        [self.dataArray removeAllObjects];
        for (NSDictionary *dic in result.data[@"data"]) {
            ByNetModel *netModel = [[ByNetModel alloc] initContentWithDic:dic];
            [self.dataArray addObject:netModel];
        }
        [self.tableView reloadData];
        
        
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
        [self.tableView.mj_header endRefreshing];
    }];
    
}



- (void)initViews{
    
    self.view.backgroundColor = BY_BACKGROUND_COLOR;
    self.tableView = [[UITableView alloc] init];
    self.tableView.backgroundColor = BY_BACKGROUND_COLOR;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        if (@available(ios 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(0);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(0);
        } else {
            make.top.equalTo(self.view).offset(0);
            make.bottom.equalTo(self.view).offset(0);
        }
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.contentView.backgroundColor = BY_BACKGROUND_COLOR;
        
    }
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    
    UIView *backgroundView = [[UIView alloc] init];
    [cell.contentView addSubview:backgroundView];
    [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cell.contentView).offset(10);
        make.right.equalTo(cell.contentView).offset(-10);
        make.bottom.equalTo(cell.contentView).offset(-10);
        make.top.equalTo(cell.contentView).offset(0);
    }];
    backgroundView.layer.cornerRadius = 10;
    backgroundView.backgroundColor = [UIColor whiteColor];
    
    UIImageView *iconView = [[UIImageView alloc] init];
    [backgroundView addSubview:iconView];
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backgroundView).offset(10);
        make.width.mas_equalTo(@60);
        make.top.equalTo(backgroundView).offset(10);
        make.bottom.equalTo(backgroundView).offset(-10);
    }];
    //iconView.backgroundColor = [UIColor redColor];
    
    
    
    UILabel *titleLabel = [[UILabel alloc] init];
    [backgroundView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconView.mas_right).offset(20);
        make.top.equalTo(iconView.mas_top).offset(5);
        make.height.mas_equalTo(@20);
        make.right.equalTo(backgroundView).offset(-10);
    }];
    
    titleLabel.font = FONT(14);
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.text = @"竞彩足球玩法介绍";
    
    
    UILabel *notesLabel = [[UILabel alloc] init];
    [backgroundView addSubview:notesLabel];
    [notesLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(backgroundView).offset(-10);
        make.bottom.equalTo(iconView.mas_bottom).offset(0);
        make.size.mas_offset(CGSizeMake(60, 20));
    }];
    notesLabel.font = FONT(12);
    notesLabel.textColor = [UIColor lightGrayColor];
    notesLabel.textAlignment = NSTextAlignmentRight;
    notesLabel.text = @"阅读50";
    
    
    UILabel *timeLabel = [[UILabel alloc] init];
    [backgroundView addSubview:timeLabel];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel.mas_left).offset(0);
        make.bottom.equalTo(iconView.mas_bottom).offset(0);
        make.right.equalTo(notesLabel.mas_left).offset(-10);
        make.height.mas_equalTo(@20);
    }];
    timeLabel.text = @"2018-07-09 11:30";
    timeLabel.font = FONT(12);
    timeLabel.textColor = [UIColor lightGrayColor];
    
    ByNetModel *netModel = [self.dataArray objectAtIndex:indexPath.row];
    [iconView sd_setImageWithURL:[NSURL URLWithString:netModel.img] placeholderImage:[ByDefaultImageView createImageFromView:self.defaultImage]];
    timeLabel.text = [NSDate getDateStringFromeNumber:[NSNumber numberWithInteger:netModel.add_time.integerValue]];
    titleLabel.text = netModel.title;
    notesLabel.text = [NSString stringWithFormat:@"阅读%@", netModel.click];
    
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ByNetModel *netModel = [self.dataArray objectAtIndex:indexPath.row];
    ByNewsDetailVC *vc = [[ByNewsDetailVC alloc] init];
    vc.netModel = netModel;
    [self by_normalPush:vc hasNavi:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
